/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MapOptionsWindow.java
 *
 * Created on 2011-12-07, 23:48:59
 */

package wumpusworld;

import ArtificialAgent.ArtificialAgent;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.JLabel;

/**
 *
 * @author Macias
 */
public class MapOptionsWindow extends javax.swing.JFrame {

    WumpusEngine wumpusEngine;
    int mapSize;
    float amountOfObstacles;
    ArtificialAgent artAgent;
    ControlWindow cWind;
    /** Creates new form MapOptionsWindow */
    public MapOptionsWindow(WumpusEngine wumpusEng,ControlWindow cWind) {
        wumpusEngine = wumpusEng;
        this.cWind = cWind;
        this.artAgent = cWind.artificialAgent;

        this.setLocation(cWind.getLocation().x, cWind.getLocation().y);

        initComponents();

        Hashtable labelTable = new Hashtable();
        labelTable.put(new Integer(1), new JLabel("Few"));
        labelTable.put(new Integer(30), new JLabel("Many"));
        amountOfObstSlider.setLabelTable(labelTable);
        amountOfObstSlider.setPaintLabels(true);
        
        amountOfObstacles=MapGenerator.obstaclesCoverageRatio;

        this.addKeyListener(new MyKeyListener(this,MyKeyListener.OPTIONS_WINDOW));
        this.setFocusable(true);
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        bOK = new javax.swing.JButton();
        bCancel = new javax.swing.JButton();
        amountOfObstSlider = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        sizeSlider = new javax.swing.JSlider();
        sizeLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Map Options");

        jLabel1.setText("Set size:");

        bOK.setText("OK");
        bOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOKActionPerformed(evt);
            }
        });

        bCancel.setText("Cancel");
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        amountOfObstSlider.setMaximum(30);
        amountOfObstSlider.setMinimum(1);
        amountOfObstSlider.setValue(5);
        amountOfObstSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                amountOfObstSliderStateChanged(evt);
            }
        });

        jLabel2.setText("Set amount of obstacles:");

        sizeSlider.setMaximum(30);
        sizeSlider.setMinimum(4);
        sizeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sizeSliderStateChanged(evt);
            }
        });

        sizeLabel.setText("0x0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(349, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(249, Short.MAX_VALUE)
                .addComponent(bOK)
                .addGap(29, 29, 29)
                .addComponent(bCancel)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(amountOfObstSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(200, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(270, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(sizeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sizeLabel)
                .addGap(156, 156, 156))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sizeLabel)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sizeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(amountOfObstSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bOK)
                    .addComponent(bCancel)))
        );

        amountOfObstSlider.setValue(
            (int)(MapGenerator.obstaclesCoverageRatio*100));
        sizeSlider.setValue(MapGenerator.sizeX);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOKActionPerformed
        // TODO add your handling code here:wumpusEngine.setMapSizeX(value);
            wumpusEngine.setMapSizeY(mapSize);
            wumpusEngine.setMapSizeX(mapSize);
            MapGenerator.obstaclesCoverageRatio=amountOfObstacles;

//            artAgent.moveCost=((2f*mapSize*cWind.greedSlider.getValue())/2000f);
            artAgent.moveCost=(((float)(cWind.greedSlider.getValue())/(2f*mapSize)));

            this.dispose();
    }//GEN-LAST:event_bOKActionPerformed

    private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_bCancelActionPerformed

    private void amountOfObstSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_amountOfObstSliderStateChanged
        // TODO add your handling code here:
        amountOfObstacles=amountOfObstSlider.getValue();
        amountOfObstacles/=100;
    }//GEN-LAST:event_amountOfObstSliderStateChanged

    private void sizeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sizeSliderStateChanged
        // TODO add your handling code here:
        mapSize=sizeSlider.getValue();
        sizeLabel.setText(sizeSlider.getValue()+"x"+sizeSlider.getValue());
    }//GEN-LAST:event_sizeSliderStateChanged

    //methods used by KeyListener

    public void increaseMapSize(){
        sizeSlider.setValue(sizeSlider.getValue()+1);
    }
    public void decreaseMapSize(){
        sizeSlider.setValue(sizeSlider.getValue()-1);
    }

    public void increaseAmountOfObstacles(){
        amountOfObstSlider.setValue(amountOfObstSlider.getValue()+1);
    }

    public void decreaseAmountOfObstacles(){
        amountOfObstSlider.setValue(amountOfObstSlider.getValue()-1);
    }
    
    public void performOkClick(){
        bOKActionPerformed(null);
    }
    
    public void performCancelClick(){
        bCancelActionPerformed(null);
    }





    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider amountOfObstSlider;
    private javax.swing.JButton bCancel;
    private javax.swing.JButton bOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel sizeLabel;
    private javax.swing.JSlider sizeSlider;
    // End of variables declaration//GEN-END:variables

}
